package com.jeethink.gs.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.jeethink.common.core.domain.AjaxResult;
import com.jeethink.common.core.domain.entity.SysUser;
import com.jeethink.common.core.page.TableDataInfo;
import com.jeethink.common.exception.job.TaskException;
import com.jeethink.gs.domain.*;
import com.jeethink.gs.domain.GsTaskDTO2;
import com.jeethink.gs.mapper.GsTaskSubMapper;
import com.jeethink.gs.utils.DateHandleUtils;
import com.jeethink.common.utils.DateUtils;
import com.jeethink.common.utils.ShiroUtils;
import com.jeethink.gs.domain.dto.GsTaskDTO1;
import com.jeethink.gs.mapper.GsTaskExecutorMapper;
import com.jeethink.gs.mapper.GsTaskScheduleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jeethink.gs.mapper.GsTaskMapper;
import com.jeethink.gs.service.GsTaskService;
import com.jeethink.common.core.text.Convert;

/**
 * 任务Service业务层处理
 * 
 * @author jeethink
 * @date 2022-08-08
 */
@Service
public class GsTaskServiceImpl implements GsTaskService
{
    @Autowired
    private GsTaskMapper gsTaskMapper;

    @Autowired
    private GsTaskExecutorMapper gsTaskExecutorMapper;

    @Autowired
    private GsTaskScheduleMapper gsTaskScheduleMapper;

    @Autowired
    private GsTaskSubMapper gsTaskSubMapper;


    /**
     * 查询任务
     * 
     * @param id 任务ID
     * @return 任务
     */
    @Override
    public AjaxResult selectGsTaskById(String id)
    {
        Map<String,Object> map=new HashMap<>();
        //任务详情信息
        map.put("task",gsTaskMapper.selectGsTaskById(id));
        //任务附件
        map.put("enclosure",gsTaskScheduleMapper.selectGsTaskScheduleByTaskId(id));
        //任务执行人
        map.put("executor",gsTaskExecutorMapper.selectGsTaskExecutorByTaskId(id));
        return AjaxResult.success(map);
    }

    /**
     * 查询任务列表
     * 
     * @param gsTask 任务
     * @return 任务
     */
    @Override
    public List<GsTask> selectGsTaskList(GsTask gsTask)
    {
        return gsTaskMapper.selectGsTaskList(gsTask);
    }

    /**
     * 新增任务
     * 
     * @param gsTask 任务
     * @return 结果
     */
    @Override
    public int insertGsTask(GsTaskDTO2 gsTask)
    {
        int Number=0;
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");

        //新增任务
        String uuid = UUID.randomUUID().toString();
        gsTask.setId(uuid);
        SysUser sysUser = ShiroUtils.getSysUser();
        gsTask.setCreateUser(sysUser.getUserId().toString());
        gsTask.setCreateTime(DateUtils.getNowDate());
        gsTask.setDelFlag("0");
        gsTask.setTaskState("0");
        if (gsTaskMapper.insertGsTask(gsTask)>0){
            Number++;
        }

        //新增任务执行人
        for (int i=0;i<gsTask.getExecutor().length;i++){
            GsTaskExecutor gsTaskExecutor=new GsTaskExecutor();
            String uuid1 = UUID.randomUUID().toString();
            gsTaskExecutor.setId(uuid1);
            gsTaskExecutor.setTaskId(uuid);
            gsTaskExecutor.setCompanyId(gsTask.getExecutor()[i]);
            gsTaskExecutor.setCreateUser(sysUser.getUserId().toString());
            gsTaskExecutor.setCreateTime(DateUtils.getNowDate());
            gsTaskExecutor.setDelFlag("0");
            gsTaskExecutorMapper.insertGsTaskExecutor(gsTaskExecutor);
        }


        //新增任务附件
        for (int i=0;i<gsTask.getEnclosure().length;i++){
            GsTaskSchedule gsTaskSchedule=new GsTaskSchedule();
            String uuid1 = UUID.randomUUID().toString();
            gsTaskSchedule.setId(uuid1);
            gsTaskSchedule.setTaskId(uuid);
            gsTaskSchedule.setTaskSchedulePath(gsTask.getEnclosure()[i]);
            gsTaskSchedule.setCreateUser(sysUser.getUserId().toString());
            gsTaskSchedule.setCreateTime(DateUtils.getNowDate());
            gsTaskSchedule.setDelFlag("0");
            gsTaskScheduleMapper.insertGsTaskSchedule(gsTaskSchedule);
        }
        GsTaskSub gsTaskSub=new GsTaskSub();
        gsTaskSub.setTaskId(gsTask.getId());
        gsTaskSub.setState("0");
        gsTaskSub.setCreateUser(sysUser.getUserId().toString());
        gsTaskSub.setCreateTime(DateUtils.getNowDate());
        gsTaskSub.setDelFlag("0");
        if (gsTask.getTaskType().equals("1")){
            String uuid1= UUID.randomUUID().toString();
            gsTaskSub.setId(uuid1);

            gsTaskSub.setStartTime(ft.format(gsTask.getTaskStartTime()));
            gsTaskSub.setEndTime(ft.format(gsTask.getTaskEndTime()));
            gsTaskSubMapper.insertGsTaskSub(gsTaskSub);
        }else{
            List list=new ArrayList();
            if (gsTask.getTaskType().equals("2")){
                list=DateHandleUtils.getMonth(gsTask.getTaskStartTime(),gsTask.getTaskEndTime());
            }else if (gsTask.getTaskType().equals("3")){
                list=DateHandleUtils.getQuarter(gsTask.getTaskStartTime(),gsTask.getTaskEndTime());
            }else{
                list=DateHandleUtils.getYear(gsTask.getTaskStartTime(),gsTask.getTaskEndTime());
            }
            for(int i=0;i<list.size();i++){
                String uuid1= UUID.randomUUID().toString();
                gsTaskSub.setId(uuid1);
                String StartTime=list.get(i)+"-01 00:00:00";
                if (i==list.size()-1){
                    gsTaskSub.setEndTime(ft.format(gsTask.getTaskEndTime()));
                }else{
                    String EndTime=list.get(i+1)+"-01 00:00:00";
                    gsTaskSub.setEndTime(EndTime);
                }
                gsTaskSub.setStartTime(StartTime);
                gsTaskSubMapper.insertGsTaskSub(gsTaskSub);
            }
        }
        return 1;
    }

    /**
     * 修改任务
     * 
     * @param gsTask 任务
     * @return 结果
     */
    @Override
    public int updateGsTask(GsTask gsTask)
    {
        return gsTaskMapper.updateGsTask(gsTask);
    }

    /**
     * 删除任务对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteGsTaskByIds(String ids)
    {
        return gsTaskMapper.deleteGsTaskByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除任务信息
     * 
     * @param id 任务ID
     * @return 结果
     */
    @Override
    public int deleteGsTaskById(String id)
    {
        return gsTaskMapper.deleteGsTaskById(id);
    }

    /**
     * 获取任务发布列表
     * @author Hyunjin
     * @date 15:11 2022/8/8
     * @param taskName
     * @return com.jeethink.common.core.page.TableDataInfo
     **/
    @Override
    public List<GsTaskDTO1> getTaskPublishing(String taskName) {
        List<GsTaskDTO1> getTaskPublishing=gsTaskMapper.getTaskPublishing(taskName);
        for (int i=0;i<getTaskPublishing.size();i++){
            getTaskPublishing.get(i).setEnclosure(gsTaskScheduleMapper.selectGsTaskScheduleByTaskId(getTaskPublishing.get(i).getId()));
            getTaskPublishing.get(i).setExecutor(gsTaskExecutorMapper.selectGsTaskExecutorByTaskId(getTaskPublishing.get(i).getId()));
        }
        return getTaskPublishing;
    }

    @Override
    public List<GsTaskDTO1> getTaskWrite(String taskType, String taskStartTime) {
        List<GsTaskDTO1> getTaskWrite=gsTaskMapper.getTaskWrite(taskType,taskStartTime);
        for (int i=0;i<getTaskWrite.size();i++){
            getTaskWrite.get(i).setEnclosure(gsTaskScheduleMapper.selectGsTaskScheduleByTaskId(getTaskWrite.get(i).getId()));
            getTaskWrite.get(i).setExecutor(gsTaskExecutorMapper.selectGsTaskExecutorByTaskId(getTaskWrite.get(i).getId()));
        }
        return getTaskWrite;
    }

    @Override
    public List<GsTaskDTO1> getTaskWriteExamine(String taskType, String taskStartTime) {
        List<GsTaskDTO1> getTaskWriteExamine=gsTaskMapper.getTaskWriteExamine(taskType,taskStartTime);
        for (int i=0;i<getTaskWriteExamine.size();i++){
            getTaskWriteExamine.get(i).setEnclosure(gsTaskScheduleMapper.selectGsTaskScheduleByTaskId(getTaskWriteExamine.get(i).getId()));
            getTaskWriteExamine.get(i).setExecutor(gsTaskExecutorMapper.selectGsTaskExecutorByTaskId(getTaskWriteExamine.get(i).getId()));
        }
        return getTaskWriteExamine;
    }

    /**
     * 修改任务状态
     * @author Hyunjin
     * @date 17:46 2022/8/9
     * @param taskState
     * @param id
     * @return com.jeethink.common.core.domain.AjaxResult
     **/
    @Override
    public AjaxResult updateTaskByState(String taskState, String id) {
        if(gsTaskMapper.updateTaskByState(taskState,id)>0){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }
}
